package 系统复习.数组;

/**
 * 给定 s 和 t 两个字符串，当它们分别被输入到空白的文本编辑器后，如果两者相等，返回 true 。# 代表退格字符。
 */

public class L844 {

    public static boolean backspaceCompare(String s, String t) {

        int sindex = -1;
        int tindex = -1;
        char[] schars = s.toCharArray();
        char[] tchars = t.toCharArray();
        for (int i = 0; i < schars.length; i++) {
            if (schars[i] == '#') {
                if (sindex >= 0) sindex--;
            } else {
                sindex++;
                if (sindex >= 0) schars[sindex] = schars[i];
            }
        }
        for (int i = 0; i < tchars.length; i++) {
            if (tchars[i] == '#') {
                if (tindex >= 0) tindex--;
            } else {
                tindex++;
                if (tindex >= 0) tchars[tindex] = tchars[i];
            }
        }

        if (sindex == tindex && sindex >= 0 && tindex >= 0) {
            for (int i = 0; i <= sindex; i++) {
                if (schars[i] != tchars[i]) {
                    return false;
                }
            }
        }
        if (tindex != sindex) return false;

        return true;
    }

    public static void main(String[] args) {
        System.out.println(backspaceCompare("ab#c", "ad#c"));
        System.out.println(backspaceCompare("y#fo##f", "y#f#o##f"));
        System.out.println(backspaceCompare("a#c", "b"));

    }

}
